package com.hiwhitley.algorithm;

/**
 * Created by hiwhitley on 2016/9/26.
 */
public class SelectSort {

    public static void main(String[] args) {
        int[] a = new int[]{2, 5, 1, 7, 10, 6, 9, 4, 3, 8};
        bubbleSort(a, 10 , 0);
        doPrint(a);
    }

    public static void selectSort(int[] a, int n, int i) {
        int j, k, tmp;
        if (i == n - 1) {
            return;
        } else {
            k = i;
            for (j = i + 1; j < n; j++) {
                if (a[j] < a[k])
                    k = j;
            }
            if (k != i) {
                tmp = a[i];
                a[i] = a[k];
                a[k] = tmp;
            }
            selectSort(a, n, i + 1);
        }
    }

    public static void bubbleSort(int[] a, int n, int i) {
        int j, tmp;
        boolean exchange;
        if (i == n - 1) {
            return;
        } else {
            exchange = false;
            for (j = n - 1; j > i; j--) {
                if (a[j] < a[j - 1]) {
                    tmp = a[j];
                    a[j] = a[j - 1];
                    a[j - 1] = tmp;
                    exchange = true;
                }
            }
            if (exchange = false) {
                return;
            } else {
            bubbleSort(a, n, i + 1);
            }

        }

    }

    public static void doPrint(int[] a) {
        for (int i : a) {
            System.out.println(i);
        }
    }
}
